package com.hello.sky.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hello.sky.annotation.AutoFill;
import com.hello.sky.enmu.OperationType;
import com.hello.sky.model.dto.DataOverViewQueryDTO;
import com.hello.sky.model.dto.OrdersPageQueryDTO;
import com.hello.sky.model.entity.Orders;
import com.hello.sky.model.vo.OrderVO;
import com.hello.sky.model.vo.SalesTop10ReportVO;
import com.hello.sky.model.vo.TurnoverReportVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Page;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author D
 */
@Mapper
public interface OrdersMapper extends BaseMapper<Orders> {

    @AutoFill(OperationType.INSERT)
    void insertOrder(Orders orders);

    @AutoFill(OperationType.UPDATE)
    void update(Orders orders);

    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(@Param("orderNumber") String outTradeNo, @Param("userId") Long userId);

    IPage<OrderVO> selectOrderWithDetailPage(@Param("dto") OrdersPageQueryDTO dto, @Param("page") IPage<?> page);

    OrderVO selectOrderWithDetailByOrderId(@Param("orderId") Long orderId);

    void cancelUpdate(Orders orders);

    @Update("update orders set status = #{orders.status} where id = #{orders.id}")
    void updateConfirm(@Param("orders") Orders orders);

    @MapKey("id")
    Map<Long, OrderVO> selectDishesByOrderIds(@Param("ids") List<Long> ids);

    void updateBatch(@Param("cancelList") List<Orders> cancelList);

    @Select("SELECT * FROM orders WHERE status = #{status} AND DATE_ADD(order_time, INTERVAL 1 HOUR) < NOW()")
    List<Orders> TimeOutDailyScan(@Param("status") Integer status);

    TurnoverReportVO turnoverStatistics(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);


    Double amountBySingleDate(@Param("begin") LocalDate begin, @Param("end") LocalDate end);

    SalesTop10ReportVO topTen(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    Integer countBySingleDate(@Param("begin") LocalDate begin, @Param("end") LocalDate end);

    Integer countValidBySingleDate(@Param("begin") LocalDate begin, @Param("end") LocalDate end, @Param("status") Integer status);

    @Select("select count(*)t from orders")
    Integer count();

    @Select("select count(*) from orders where status = #{status}")
    Integer countValit(Integer status);

    List<Orders> countByStatusAndSingalDate(@Param("localDateTime") LocalDateTime localDateTime, @Param("status") Integer status);

    Integer dateRangeAllCount(DataOverViewQueryDTO dto);

    Integer dateRangeValidCount(@Param("dto") DataOverViewQueryDTO dto, @Param("status") Integer status);
}
